package com.quanshu.shop.goods.view;

import android.content.Context;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.quanshu.shop.goods.adapter.SpecialGoodsAdapter;
import com.quanshu.shop.goods.entity.SpecialGoodsInfo;
import com.quanshu.shop.main.api.ShopMainIF;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SpecialGoodsActivity extends BaseActivity {
    public static final String SPECIAL_ID = "special_id";
    @BindView(R.id.special_title)
    BaseTitleBar specialTitle;
    @BindView(R.id.special_img)
    ImageView specialImg;
    @BindView(R.id.special_line1)
    View specialLine1;
    @BindView(R.id.special_txt)
    TextView specialTxt;
    @BindView(R.id.number_text1)
    TextView numberText1;
    @BindView(R.id.number_text2)
    TextView numberText2;
    @BindView(R.id.number_text3)
    TextView numberText3;
    @BindView(R.id.number_text_ll)
    LinearLayout numberTextLl;
    @BindView(R.id.special_body)
    ConstraintLayout specialBody;
    @BindView(R.id.special_rl)
    RecyclerView specialRl;
    private String specialId;
    private Context mCotext;
    private SpecialGoodsAdapter specialGoodsAdapter;
    private SpecialGoodsInfo specialGoodsInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_goods);
        mCotext = this;
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        ImmersionBar.with(this).titleBar(specialTitle).statusBarColor("#FFFFFF").statusBarDarkFont(true).init();
        Bundle b = getIntent().getExtras();
        specialId = b.getString(SPECIAL_ID);
        specialTitle.setLeftImage(R.drawable.icon_back_black_2_0).setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        specialRl.setLayoutManager(gridLayoutManager);
        specialRl.setNestedScrollingEnabled(false);
        if (!TextUtils.isEmpty(specialId)) {
            getSpecialInfo();
        }
    }

    /**
     * 获取特殊商品信息
     */
    private void getSpecialInfo() {
        NetClient.getInstance()
                .getShopService(ShopMainIF.class)
                .getSpecialInfo(specialId)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<SpecialGoodsInfo>>() {
                    @Override
                    public void onSuccess(BaseResponse<SpecialGoodsInfo> response) {
                        if (response.getData() != null) {
                            specialGoodsInfo = response.getData();
                            specialTitle.setCenterText(response.getData().getSpecial_name());
                            Glide.with(SpecialGoodsActivity.this).load(Config.URL_HOST_SHOP + response.getData().getMob_detailpic()).into(specialImg);
                            specialTxt.setText("        " + response.getData().getContent());
                            if (response.getData().getGoods() != null) {
                                Log.d("特殊商品数据", "onSuccess: " + response.getData().getGoods().size());
                                numberText2.setText(String.valueOf(response.getData().getGoods().size()));
                                specialGoodsAdapter = new SpecialGoodsAdapter(response.getData().getGoods(), mCotext);
                                specialGoodsAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                    @Override
                                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                                        if (MyApplication.getInstance().getCurrentUser() == null) {
//                                            BaseTool.goActivity(mCotext, LoginActivity.class, null);
//                                            return;
//                                        }
                                        switch (view.getId()) {
                                            case R.id.goods_list_cl:
                                                if (specialGoodsInfo.getGoods().get(position) != null) {
                                                    Bundle bundle = new Bundle();
                                                    bundle.putString("goods_info_id", specialGoodsInfo.getGoods().get(position).getId());
                                                    BaseTool.goActivity(mCotext, GoodsInfoActivity.class, bundle);
                                                }
                                                break;
                                        }
                                    }
                                });
                                specialRl.setAdapter(specialGoodsAdapter);
                            }
                        }

                    }
                });
    }

}
